package com.xyz.threadPoolAndOther;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author 肖咉卓
 *
 * 异步回调
 */
public class CompletableFutureDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //异步没有返回值
        CompletableFuture<Void> completableFuture1=  CompletableFuture.runAsync(() -> {
            System.out.println(Thread.currentThread().getName()+"\t 没有返回值，update mysql ok");
        });
        System.out.println(completableFuture1.get());

        //异步有返回值
        CompletableFuture<Integer> completableFuture2 =CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName()+"\t 有返回值，insert mysql ok");
            //int age = 10/0;
            return 1024;
        });
        //获得返回值
        System.out.println(completableFuture2.whenComplete((t,u) ->  {
            System.out.println("*****t:"+t);
            System.out.println("*****u:"+u);
        }).exceptionally(f -> {
            //当报错时要执行的，和返回
            System.out.println("***excption:"+f.getMessage());
            return 444;
        }).get());
    }
}
